package at.ac.tuwien.mg.webservice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.AsyncWebResource;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

import at.ac.tuwien.mg.bean.Item;
import at.ac.tuwien.mg.bean.Offer;

@Path("/supplier1")
public class Supplier1 {

	private static String supplierId = "Supplier1";
	private static Map<String, Integer> stock = new HashMap<String, Integer>();
	static
	{
		stock.put("gold",1000);
		stock.put("stone",1000);
		stock.put("silver",1000);
		stock.put("wood",1000);
	}
		
	@POST
	@Path("offer")
	@Produces(MediaType.APPLICATION_XML)
	public void getOffer(Offer offer) throws InterruptedException {
		System.out.println(supplierId + " >> calculating offer for: " + offer.getItems().size() + " items");
		
		int itemsFound = 0;

		for(Item i : offer.getItems()) {
			if(stock.containsKey(i.getGood())){
				int onStock = stock.get(i.getGood());
				if(onStock >= i.getAmount()) {
					double pricePerItem = new Double(Math.random()*100);
					i.setPrice(pricePerItem);
					itemsFound++;
				}
				else {
					//not enough in stock
				}
			}
			else {
				//good not in stock
			}
		}
		
		if(itemsFound == offer.getItems().size()) {
			//send it back to new webservice
			Thread.sleep(5000);
			ClientConfig cc = new DefaultClientConfig();
			Client c = Client.create(cc);
			AsyncWebResource r = c.asyncResource(offer.getReturnAdress());
			r.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE);
			System.out.println("Send offer to offerListener from supplier: " + supplierId);
			r.post(offer);
		}
	}

	@POST
	@Path("order")
	@Consumes(MediaType.APPLICATION_XML)
	@Produces(MediaType.APPLICATION_XML)
	public void orderOffer(Offer offer) {
		System.out.println("things are getting orderd - size: " + offer.getItems().size());
		
		List<Item> itemsToOrder = offer.getItems();
		for(Item item : itemsToOrder){
			if(stock.containsKey(item.getGood())){						//item in stock?
				int onStock = stock.get(item.getGood());				
				if(onStock >= item.getAmount()) {						//enough in stock?
					stock.put(item.getGood(),onStock-item.getAmount());	//reduce stock
				} else {
					itemsToOrder.remove(item);							//remove from order list -> item will not be returned in the confirmation
					System.out.println("not enough amount in stock for item: " + item.getGood());
				}
			} else {
				itemsToOrder.remove(item);								//remove from order list -> item will not be returned in the confirmation
				System.out.println("item: " + item.getGood() + " not in stock!");
			}
		}
	}
	
	@GET
	@Path("test")
	@Produces(MediaType.TEXT_PLAIN)
	public String test() {
		return "worked!";
	}
	
	public static String getSupplierId() {
		return supplierId;
	}

}
